package com.exahq.controller;

import cn.hutool.core.io.resource.ClassPathResource;
import cn.hutool.http.HttpResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.exahq.common.lang.Result;
import com.exahq.entity.*;
import com.exahq.service.*;
import com.exahq.util.IdCode;
import com.exahq.util.ReadUtils;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.*;

@RestController
@RequestMapping("/utils")
public class UtilsController {
    @Autowired
    ViweService viweService;
    @Autowired
    OrganizationsService organizationsService;
    @Autowired
    CompanydataService companydataService;
    @Autowired
    LoginlogService loginlogService;
    @Autowired
    PartnerinfoService partnerinfoService;
    @Autowired
    CompanytypeService companytypeService;
    @Autowired
    IndustryService industryService;


    /**
     * 民族数据读取
     * @return
     */
    @GetMapping("/nationList")
    public Result nationList() {
        ClassPathResource resource = new ClassPathResource("/static/nation.json");
        File filePath = resource.getFile();
        String s = ReadUtils.readJsonFile(filePath);
        JSONObject jobj =  JSONObject.fromObject(s);
        return Result.succ(jobj);
    }

    /**
     * 访问量
     * @return
     */
    @GetMapping("/views")
    public Result views() {
        Viwe viwe =viweService.getById(1);
        return Result.succ(viwe);
    }

    /**
     * 公司类型统计
     * @return
     */
    @GetMapping("/company")
    public Result company() {
      /* QueryWrapper<Organizations> queryWrapper=new QueryWrapper();
        queryWrapper.select("organization");
        List<Organizations> organizationsList =organizationsService.selectList(queryWrapper);*/
        List<Integer> list =companydataService.getSumNumber();
        System.err.println(list);
        List<String> organizationsList =organizationsService.getList();
        Map<Object,Object> map =new HashMap<Object,Object>() ;
        map.put("list",list);
        map.put("organizationsList",organizationsList);
        return Result.succ(map);
    }

    /**
     *浏览量统计表
     * @param id
     * @return
     */
    @GetMapping("/viewList{id}")
    public Result viewList(@PathVariable(name = "id") Integer id) {
       List<Loginlog> loginlogList =loginlogService.selectLists(id);
        return Result.succ(loginlogList);
    }

    @GetMapping("/viewSave")
    public Result viewSave() {
        Viwe viwe =viweService.getById(1);
        viwe.setViwes(viwe.getViwes()+1);
        return Result.succ(viweService.saveOrUpdate(viwe));
    }


    /**
     * 验证码
     * @param response
     * @return
     */
    @GetMapping("/sendCode")
    public Result sendCode(HttpServletResponse response) {
        String result=IdCode.getNonce_str();
        System.out.println(result);
        Cookie userCookie=new Cookie("code",result);
        userCookie.setMaxAge(60);
        userCookie.setPath("/");
        response.addCookie(userCookie);
        return Result.succ(result);
    }

    /**
     * 根据公司查询法人
     * @param id
     * @return
     */
    @GetMapping("/byPid/{id}")
    public Result byPid(@PathVariable(name = "id") Integer id) {
        Companydata companydata =companydataService.getById(id);
        QueryWrapper<Partnerinfo> queryWrapper =new QueryWrapper<Partnerinfo>();
        queryWrapper.eq("cid",id);
        queryWrapper.eq("pid",companydata.getUid());
        queryWrapper.eq("del_flag",1);
        Partnerinfo partnerinfo =partnerinfoService.getOne(queryWrapper);
        Map<Object,Object> map =new HashMap<Object,Object>();
        if (partnerinfo == null){
            map.put("pid",companydata.getUid());
            map.put("flag",true);
            return Result.succ(map);
        }
        map.put("pid",null);
        map.put("flag",false);
        return Result.succ(map);
    }

    /**
     * 微信小程序company查询条件
     * @return
     */
    @GetMapping("/selectData")
    public Result selectData() {

        List<Organizations> organizationsList =organizationsService.list();//组织机构
        List<Companytype> companytypeList = companytypeService.list(); //企业类型
        List<Industry> industryList =industryService.list();//所属行业
        Map<Object,Object> map =new HashMap<Object,Object>();
        map.put("organizationsList",organizationsList);
        map.put("companytypeList",companytypeList);
        map.put("industryList",industryList);
        return Result.succ(map);
    }
}
